/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import data.UserData;
import data.AdminData;
/**
 *
 * @author brlnt
 */
public class LoginDB extends ConnectDB {
    private String sql;
    private UserData dataUser;
    private AdminData dataAdmin;
;    
    public boolean cekLogin(String ID, String password) throws SQLException, ClassNotFoundException{
        Connection con = createConnection();
        Statement stmt = con.createStatement();
        sql = "SELECT * from `user` WHERE id='"+ID+"' AND password='"+password+"';";
        ResultSet rs = stmt.executeQuery(sql);
        if(rs.next()){
            rs.close();
            stmt.close();
            con.close();
            return true;
        }else{
            rs.close();
            stmt.close();
            con.close();
            return false;
        }
    }
    
    public boolean cekLoginAdmin(String ID,String username, String password, String label) throws SQLException, ClassNotFoundException{
        Connection con = createConnection();
        Statement stmt = con.createStatement();
        sql = "SELECT * from `administrator` WHERE IDmimin='"+ID+"'AND usernamemimin='"+username+"' AND passwordmimin='"+password+"'  AND labeladmin='"+label+"';";
        ResultSet rs = stmt.executeQuery(sql);
        if(rs.next()){
            rs.close();
            stmt.close();
            con.close();
            return true;
        }else{
            rs.close();
            stmt.close();
            con.close();
            return false;
        }
    }
       
    public UserData getUserById(String ID) throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `user` WHERE ID='"+ID+"';";
        return getUser(sql);
    }
    
    public AdminData getAdminById(String ID) throws SQLException, ClassNotFoundException{
        sql = "SELECT * from `administrator` WHERE IDmimin='"+ID+"';";
        return getAdmin(sql);
    }
    
    public UserData getUser(String sql) throws SQLException, ClassNotFoundException{
        Connection con = createConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        dataUser = new UserData();
        while(rs.next()){
            dataUser.setID(rs.getString(1));
            dataUser.setFirstname(rs.getString(2));
            dataUser.setLastname(rs.getString(3));
            dataUser.setEmail(rs.getString(4));
            dataUser.setPassword(rs.getString(5));     
        }
        return dataUser;
    }
    
    public AdminData getAdmin(String sql) throws SQLException, ClassNotFoundException{
        Connection con = createConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        dataAdmin = new AdminData();
        while(rs.next()){
            dataAdmin.setID(rs.getString(1));
            dataAdmin.setUsername(rs.getString(2));
            dataAdmin.setFirstname(rs.getString(3));
            dataAdmin.setLastname(rs.getString(4));
            dataAdmin.setEmail(rs.getString(5));
            dataAdmin.setPassword(rs.getString(6));
            dataAdmin.setLabel(rs.getString(7));
            dataAdmin.setID2(rs.getString(8));
        }
        return dataAdmin;
    }
}
